<?php

namespace User\Controller;
use Think\Controller;
class MyAccountController extends Controller
{
    //个人资料
    public function My_info(){
        $user = session('user');
        if (empty($user)) {
            redirect('Index/login', 1, '身份信息过期，正在跳转登录页面...');
        } else {
            $data = M('user')->where(array('user.user_id' => $user['user_id']))
                ->join('position on user.position=position.id','left')->find();
            if($data['status']==1){
                $data['status']="未认证";
            }else if($data['status']==2){
                $data['status']="已申请";
            }else if($data['status']==3){
                $data['status']="已核实";
            }else if($data['status']==4){
                $data['status']="未通过";
            }
            $time = strtotime($data['user_birthday']);
            $data['user_birthday'] = date('Y年m月d日',$time);
            $this->assign('data', $data);
            $this->display();
        }
    }
    //上传头像
    public function My_head(){
        $user = session('user');
        if(empty($user)){
            echo "身份信息过期，请重新登录";exit;
        }
        $user_id = $user['user_id'];
        $upload = new \Think\Upload();// 实例化上传类
        $upload->autoSub = false;
        $upload->maxSize = 20145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg', 'JPG');    // 设置附件上传类型
        $upload->rootPath = './Uploads/';                     // 设置附件上传根目录
        $upload->savePath = 'user_head/'.$user_id.'/';                                  // 设置附件上传（子）目录
        $info = $upload->upload();

        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功 获取上传文件信息
            $head_image = 'Uploads/' .$info['file']['savepath']. $info['file']['savename'];
            $img_up = M('user')->where(array('user_id' =>$user_id))->save(array('image' =>$head_image));
            if ($img_up !== false) {
                echo "上传成功";
            } else {
                echo "上传失败";
            }
        }
    }
    //账号信息
    public function My_data(){
        $user = session('user');
        if (empty($user)) {
            redirect('Index/login', 1, '身份信息过期，正在跳转登录页面...');
        } else {
            $data = M('user')->where(array('user_id' => $user['user_id']))
                ->join('position on user.position=position.id','left')
                ->find();
            if($data['status']==1){
                $data['state']="未认证";
            }else if($data['status']==2){
                $data['state']="已申请";
            }else if($data['status']==3){
                $data['state']="已核实";
            }else if($data['status']==4){
                $data['state']="未通过";
            }
            $time = strtotime($data['user_birthday']);
            $data['user_birthday'] = date('Y年h月d日',$time);

            $position = M('position')->select();
            $position_apply = M('position_apply')->where(array('mt4'=>$user['user_id']))->order('id desc')->select();  //申请职位
            foreach ($position_apply as $k=>$v){
                $position_apply[$k]['apply_time'] = date('Y-m-d H:i:s',$v['apply_time']);
                if($v['state']==1){
                    $position_apply[$k]['state']="<font color=#4682b4>待审核</font>";
                }elseif($v['state']==2){
                    $position_apply[$k]['state']="<font color=green>已同意</font>";
                }elseif($v['state']==3){
                    $position_apply[$k]['state']="<font color=red>已拒绝</font>";
                }
                foreach($position as $key=>$value){
                    if($value['id']==$v['currently']){
                        $position_apply[$k]['currently'] =$value['user_position'];
                    }
                    if($value['id']==$v['apply']){
                        $position_apply[$k]['apply']  = $value['user_position'];
                    }
                }
            }
            $lever=M('lever_apply')->where(array('mt4'=>$user['user_id']))->select();

            $this->assign('lever', $lever);
            $this->assign('postition_apply', $position_apply);
            $this->assign('postition', $position);
            $this->assign('data', $data);
            $this->display();
        }
    }
    //删除文件重新上传
    public function img_delete(){
        $user_id = I('id');
        $front_img =I('front');
        $behind_img =I('behind');
        $hand_img    =  I('hand');

        if(empty($user_id)){
            $this->ajaxReturn(array('error'=>"账号信息过期",'status'=>0));exit;
        }else{
            $de = unlink($front_img);     //删除文件
            $de = unlink($behind_img);     //删除文件
            $de = unlink($hand_img);     //删除文件
           $save = M('user')->where(['user_id'=>$user_id])->save(['front_img'=>0,'behind_img'=>0,'hand_img'=>0,'status'=>1]);
           if($save!==false){
               if($save!==false){
                   $this->ajaxReturn(array('msg'=>"删除成功",'status'=>1));
               }else{
                   $this->ajaxReturn(array('error'=>"删除失败",'status'=>0));
               }
           }
        }

    }
    //身份证上传
    public function img_upload(){
        $user = session('user');
        if (empty($user)) {
            echo "身份信息过期，请重新登录";exit;
        }

        if(empty($_FILES['front_img']['name'])){
            die("银行卡正面未上传");
        }
        if(empty($_FILES['hand_img']['name'])){
            die("身份证正面未上传");
        }
        if(empty($_FILES['behind_img']['name'])){
            die("身份证背面未上传");
        }
        $upload = new \Think\Upload();// 实例化上传类
        $upload->autoSub = false;
        $upload->maxSize = 0;   // 设置附件上传大小 0为不限大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg', 'JPG');    // 设置附件上传类型
        $upload->rootPath = './Uploads/';                         // 设置附件上传根目录
        $upload->savePath ='ID_card/'.$user['user_id'].'/';                                   // 设置附件上传（子）目录
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {
            //上传成功 获取上传文件信息
            $data['front_img']  = 'Uploads/' . $info['front_img']['savepath'] . $info['front_img']['savename'];
            $data['hand_img']   = 'Uploads/' . $info['hand_img']['savepath'] . $info['hand_img']['savename'];
            $data['behind_img'] = 'Uploads/' . $info['behind_img']['savepath'] . $info['behind_img']['savename'];
            $data['status']=2;
            if(empty($front_img) && empty($hand_img) && empty($behind_img)){
                $user = M('user')->where(['user_id'=>$user['user_id']])->save($data);
                if($user!==false){
                    echo "上传成功,请等待管理员审核";
                }else{
                    echo "上传失败,请联系管理员解决";
                }
            }
        }
    }
    //发送验证码
    public function very_send(){
        $user = session('user');
        $type_sand = I('data')?I('data'):die($this->ajaxReturn(array('error'=>"发送失败，请稍后再试",'status'=>0)));
        if(empty($user)){$this->ajaxReturn(array('error'=>"身份信息过期",'status'=>0));exit;}  //session为空退出操作
        $data = M('user')->where(['user_id'=>$user['user_id']])->field('user_id,user_email,user_phone')->find();
        if(!empty($data)){
            $send['code']   = rand(pow(10,(6-1)), pow(10,6)-1);//验证码生成
            $send['time']   = time();
            $send['mail']   = $type_sand;
            $send['mt4']    = $user['user_id'];
            $add  = M('forget_pwd_code')->add($send);                       //验证码写入数据库
            if($add){
                //使用邮件发送
                if($data['user_email']==$type_sand){
                    $mould = M('mail_mould')->where(['type'=>10010])->find();
                    $msg = str_replace('{1}',$send['code'],$mould['content']);
                    $send = SendMail($user['user_email'], $mould['title'], htmlspecialchars_decode($msg));
                //使用手机发送
                }else{
                    $send = 1;
                }
                if($send){
                    $this->ajaxReturn(array('msg'=>"发送成功，3分钟内有效",'status'=>1));
                }else{
                    $this->ajaxReturn(array('error'=>"发送失败，请稍后再试",'status'=>0));
                }
            }else{
                $this->ajaxReturn(array('error'=>"发送网络错误,请稍后再试",'status'=>0));exit;
            }
        }else{
            $this->ajaxReturn(array('error'=>"身份信息错误",'status'=>0));exit;
        }
    }
    //验证
    public function Verification(){
        $user = session('user');
        $verify = I('post.very')?I('post.very'):'';
        $time = time();
        if(empty($user)){$this->ajaxReturn(array('error'=>"身份信息过期",'status'=>0));exit;}  //session为空退出操作
        $userData = M('user')->where(array('user_id'=>$user['user_id']))->field('user_id,user_email,user_phone')->find();
        $query = M('forget_pwd_code')->where(array('mt4'=>$userData['user_id']))->order('id desc')->find();
        if($query['code']==$verify){
            $mins = (($time-$query['time'])%3600)/60;
            if($mins<3){
                $this->ajaxReturn(array('msg'=>"验证成功",'status'=>1));
            }else{
                $this->ajaxReturn(array('error'=>"验证码失效",'status'=>0));
            }
        }else{
            $this->ajaxReturn(array('error'=>"验证码错误",'status'=>0));
        }
    }
    //修改密码
    public function password_save(){
        $user = session('user');
        if(empty($user)){$this->ajaxReturn(array('error'=>"身份信息过期",'status'=>0));exit;}  //session为空退出操作
        $pwd = I('pwd')? I('pwd'):die($this->ajaxReturn(array('error'=>"密码不能为空",'status'=>0)));
        $repwd = I('repwd')? I('repwd'):die($this->ajaxReturn(array('error'=>"确认密码不能为空",'status'=>0)));
        if($pwd==$repwd){
            $pwd = md5(md5($pwd));
            $save = M('user')->where(array('user_id'=>$user['user_id']))->save(['user_password'=>$pwd]);
            if($save!=false){
                $this->ajaxReturn(array('msg'=>"修改成功",'status'=>1));
            }else{
                $this->ajaxReturn(array('error'=>"修改时不得和原密码一致",'status'=>0));
            }
        }else{
            $this->ajaxReturn(array('error'=>"输入不一致",'status'=>0));
        }
    }
    //修改MT4密码修改
    public function mt4_password_save(){
        $user = session('user');
        if(empty($user)){$this->ajaxReturn(array('error'=>"Operation error",'status'=>0));exit;}  //session为空退出操作
        $pwd = I('pwd')? I('pwd'):die($this->ajaxReturn(array('error'=>"密码不能为空",'status'=>0)));
        $repwd = I('repwd')? I('repwd'):die($this->ajaxReturn(array('error'=>"确认密码不能为空",'status'=>0)));
        if($pwd==$repwd){
            $login = $user['user_id'];
            if($user['type']==1){
                $request = "action=changepassword&login=$login&pass=$pwd&investor=$pwd\0";
                $answer=mt4_operation_digital($request);
            }else{
                $request = "action=changepassword&login=$login&pass=$pwd&investor=$pwd\0";
                $answer=mt4_operation($request);
            }

            $answer = explode('&', $answer);
            $data = [];
            foreach ($answer as $k => $v) {
                $arr = explode('=', $v);
                $data[$arr[0]] = $arr[1];
            }
            if ($data['result'] == 1) {
                $this->ajaxReturn(array('msg'=>"修改成功1",'status'=>1));
            } else {
                $this->ajaxReturn(array('error'=>"修改失败",'status'=>0));
            }

        }else{
            $this->ajaxReturn(array('error'=>"输入不一致",'status'=>0));
        }


    }

    //查看职位申请详情
    public function Apply_details(){
        $id = I('id');
        if(empty($id)){
            $this->ajaxReturn(array('error'=>"没有信息,请重试",'status'=>0));exit;
        }else{
            $data = M('position_apply')->where(['id'=>$id])->find();
            if(!empty($data)){
                if($data['state']==1){
                    $data['status']="<font color=#4682b4>待审核</font>";
                }elseif($data['state']==2){
                    $data['status']="<font color=green>已同意</font>";
                }elseif($data['state']==3){
                    $data['status']="<font color=red>已拒绝</font>";
                }
                $data['apply_time']=date('Y-m-d H:i:s',$data['apply_time']);
                $data['hand_time']=date('Y-m-d H:i:s',$data['hand_time']);

                $this->assign('data',$data);
                $this->display();
            }else{
                $this->ajaxReturn(array('error'=>"Data error",'status'=>0));exit;
            }
        }
    }
    //职位申请
    public function position_apply(){
       $user = session('user');
       if(empty($user)){$this->ajaxReturn(array('error'=>"身份过期",'status'=>0));exit;}  //session为空退出操作
       $position = I('apply_position')? I('apply_position'):die($this->ajaxReturn(array('error'=>"申请的职位不能为空",'status'=>0)));
       $notes = I('notes')? I('notes'):die($this->ajaxReturn(array('error'=>"详细说明不能为空",'status'=>0)));
       $query = M('position_apply')->where(array('mt4'=>$user['user_id']))->select();
       foreach ($query as $k =>$v){
           if($v['state']==1){
               $this->ajaxReturn(array('error'=>"当前还有未审核的申请",'status'=>0));break;
           }
       }
       $data = M('user')->where(['user_id'=>$user['user_id']])->field('user_id,user_email,user_name,position')->find();
       if(!empty($data)){
           $add['mt4']        = $user['user_id'];
           $add['user_email'] = $data['user_email'];
           $add['name']       = $data['user_name'];
           $add['currently']  = $data['position'];
           $add['apply']      = $position;
           $add['notes']      = $notes;
           $add['apply_time'] = time();
           $add['state']      = 1;
           $addData = M('position_apply')->add($add);
           if($addData){
               $this->ajaxReturn(array('msg'=>"申请提交成功",'status'=>1));
           } else {
               $this->ajaxReturn(array('error'=>"申请提交失败",'status'=>0));
           }
       }else{
           $this->ajaxReturn(array('error'=>"提交失败",'status'=>0));
       }
    }
    //删除申请
    public function Apply_positionDelete(){
        $id = I('id');
        if(empty($id)){
            $this->ajaxReturn(array('error'=>"没有信息,请重试",'status'=>0));exit;
        }else{
            $de = M('position_apply')->where(array('id'=>$id))->delete();
            if($de){
                $this->ajaxReturn(array('msg'=>"删除成功",'status'=>1));
            } else {
                $this->ajaxReturn(array('error'=>"删除失败",'status'=>0));
            }
        }
    }
   //查看杠杆申请详细
    public function Apply_lever(){
        $id = I('id')?I('id'):die("信息为空");
        $data = M('lever_apply')->where(['id'=>$id])->find();
        if($data['state']==1){
            $data['status']="<font color=#4682b4>待审核</font>";
        }elseif($data['state']==2){
            $data['status']="<font color=green>已同意</font>";
        }elseif($data['state']==3){
            $data['status']="<font color=red>已拒绝</font>";
        }
        $data['apply_time'] = timeFormat($data['apply_time']);
        if($data['hand_time']==''){
            $data['hand_time']='-- --';
        }else{
            $data['hand_time'] = timeFormat($data['hand_time']);
        }
        $this->assign('data',$data);
        $this->display();
    }
    //杠杆申请提交
    public function apply_lever_add(){
        $user = session('user');
        if(empty($user)){$this->ajaxReturn(array('error'=>"身份过期",'status'=>0));exit;}
        $lever = I('apply_lever')? I('apply_lever'):die($this->ajaxReturn(array('error'=>"申请的杠杆不能 0",'status'=>0)));
        $notes = I('notes')? I('notes'):die($this->ajaxReturn(array('error'=>"详细说明不能为空",'status'=>0)));

        $query = M('lever_apply')->where(array('mt4'=>$user['user_id'],'state'=>1))->select();
        if(!empty($query)){ $this->ajaxReturn(array('error'=>"当前还有未审核的申请",'status'=>0));exit;}
        $data = M('user')->where(['user_id'=>$user['user_id']])->field('user_id,user_email,user_name,level')->find();
        if(!empty($data)){
            $add['mt4']        = $data['user_id'];
            $add['user_email'] = $data['user_email'];
            $add['user_name']  = $data['user_name'];
            $add['current_lever'] = $data['level'];
            $add['apply_lever']   = $lever;
            $add['notes']      = $notes;
            $add['apply_time'] = time();
            $add['state']      = 1;
            $addData = M('lever_apply')->add($add);
            if($addData){
                $this->ajaxReturn(array('msg'=>"申请提交成功",'status'=>1));
            } else {
                $this->ajaxReturn(array('error'=>"申请提交失败",'status'=>0));
            }
        }else{
            $this->ajaxReturn(array('error'=>"Data error",'status'=>0));
        }

    }
    //杠杆申请删除
    public function Apply_LeverDelete(){
        $id = I('id');
        if(empty($id)){
            $this->ajaxReturn(array('error'=>"没有信息,请重试",'status'=>0));exit;
        }else{
            $de = M('lever_apply')->where(array('id'=>$id))->delete();
            if($de){
                $this->ajaxReturn(array('msg'=>"删除成功",'status'=>1));
            } else {
                $this->ajaxReturn(array('error'=>"删除失败",'status'=>0));
            }
        }
    }


}